package com.atguigu.gmall.all.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: 李旭
 * @create: 2021-09-01 10:26
 *
 * 首页
 **/
@Controller
public class IndexController {

    @Autowired
    private ProductFeignClient productFeignClient;

    //访问首页  http://www.gmall.com  首页
    @GetMapping("/")
    public String index(Model model){
        List<Map> category1IdListMap = getData();
        //2:数据回显
        model.addAttribute("list",category1IdListMap);
        return "index/index";
    }

    @Autowired
    private TemplateEngine templateEngine;//Thymeleaf 的模板引擎  生成Html

    //生成Html页面  首页
    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml(){
        //1:查询数据
        List<Map> category1IdListMap = getData();
        Context context = new Context();
        context.setVariable("list",category1IdListMap);
        //2:准备模板   index/index
        //3:生成Html页面  指定文件夹中
        //参数1：模板名称
        //参数2：数据
        //参数3：输出流
        Writer writer = null;
        //乱码   读： 写： 编码不一致
        try {//Centos7  /usr/local   Windows10  字符流 字节流
     /*       writer = new OutputStreamWriter(
                    new FileOutputStream("D:\\temp\\index.html")
                    ,"UTF-8"); */
            writer = new PrintWriter("D:\\temp\\index.html","UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //读：读取了模板 UTF-8
        templateEngine.process("index/index",context,writer);
        return Result.ok();
    }

    //获取数据
    public List<Map> getData(){
        //1:准备数据    远程调用商品微服务
        List<BaseCategoryView> baseCategoryViewList =
                productFeignClient.getBaseCategoryViewList();//1000+
        //Stream流 分组功能
        //Map  K:Long 分组字段 一级分类ID getCategory1Id    1       2      3
        //Map  V:List<BaseCategoryView>                   1-60    61-85  85~...
        Map<Long, List<BaseCategoryView>> category1IdMapGroup  = baseCategoryViewList.
                stream().collect(
                Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        //1.1:准备页面上所需要的数据结构  无敌版对象 Map .put(自定义
        List<Map> category1IdListMap = new ArrayList<>();//17长度
        int index = 1;
        for (Map.Entry<Long, List<BaseCategoryView>> category1IdEntry : category1IdMapGroup.entrySet()) {
            List<BaseCategoryView> category1IdList = category1IdEntry.getValue();
            Map category1IdMap = new HashMap();
            category1IdMap.put("index",index++);
            category1IdMap.put("categoryId",category1IdEntry.getKey());
            category1IdMap.put("categoryName",category1IdList.get(0).getCategory1Name());
            //////////////////////////////////////////////////////////////////
            List<Map> category2IdListMap = new ArrayList<>();
            Map<Long, List<BaseCategoryView>> category2IdMapGroup = category1IdList.stream().collect(Collectors.
                    groupingBy(BaseCategoryView::getCategory2Id));
            for (Map.Entry<Long, List<BaseCategoryView>> category2IdEntry : category2IdMapGroup.entrySet()) {
                List<BaseCategoryView> category2IdList = category2IdEntry.getValue();
                Map category2IdMap = new HashMap();
                category2IdMap.put("categoryId",category2IdEntry.getKey());
                category2IdMap.put("categoryName",category2IdList.get(0).getCategory2Name());
                ///////////////////////////////////////////////////////////////////////////
                List<Map> category3IdListMap = new ArrayList<>();
                for (BaseCategoryView category3Id : category2IdList) {
                    Map category3IdMap = new HashMap();
                    category3IdMap.put("categoryId",category3Id.getCategory3Id());
                    category3IdMap.put("categoryName",category3Id.getCategory3Name());
                    category3IdListMap.add(category3IdMap);
                }
                category2IdMap.put("categoryChild",category3IdListMap);
                category2IdListMap.add(category2IdMap);
            }
            category1IdMap.put("categoryChild",category2IdListMap);
            //将一级分类加入到容器中
            category1IdListMap.add(category1IdMap);
        }
        return category1IdListMap;
    }

}
